/**
 * https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/
 * 判断二叉树的后序遍历
 * @param postorder 
 */
const verifyPostorder = (postorder: number[]): boolean => {
    // 1. 找到比最后一个节点大的值
    const len = postorder.length;
    const rootVal = postorder[len - 1]

    let pos = -1
    for (let i = 0; i < len; i++) {
        if (postorder[i] > rootVal) {
            pos = i
            break;
        }
    }

    // 2. 判断从pos到处理最后一个元素的位置是不是都大于rootVal
    const postArr = postorder.slice(pos, len - 1)

    const has = postArr.every(c => c > rootVal)
    // 存在不大于根元素的, 直接返回false
    if (!has) return false

    // 3. 递归遍历左和右子树
    return verifyPostorder(postorder.slice(0, pos)) || verifyPostorder(postArr)
};